package spzx.controller;


import com.atguigu.spzx.model.dto.system.AssginMenuDto;
import com.atguigu.spzx.model.entity.system.SysMenu;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.SysMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import spzx.service.SysMenuService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/sysMenu")
public class SysMenuController {

    @Autowired
    SysMenuService sysMenuService;

    /**
     * 查询当前用户列表
     * @return 菜单列表
     */
    @GetMapping("findCurrentUserMenuList")
    public Result findCurrentUserMenuList(){
        //直接返回的是一级菜单列表
        List<SysMenuVo> list = sysMenuService.findCurrentUserMenuList();
        //xxx.build()方法使用场合及作用：构建一个Result对象，并返回给前端
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }




    /**
     * 为角色分配菜单
     * @param assginMenuDto
     * @return
     */
    @PostMapping("doAssignMenu")
    public Result doAssignMenu(@RequestBody AssginMenuDto assginMenuDto){
        sysMenuService.sysMenuService(assginMenuDto);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询菜单列表树 +  当前角色已分配的菜单id列表（不包括is_half=1）
     * @param roleId
     * @return
     */
    @GetMapping("findMenuListAndMenuIdList/{roleId}")
    public Result findMenuListAndMenuIdList(@PathVariable Long roleId){
        List<SysMenu> menuList = sysMenuService.findMenuListByParentId(0L);
        List<Long> menuIdList = sysMenuService.findMenuIdListByRoleId(roleId);
        Map map = new HashMap();
        map.put("menuList",menuList);
        map.put("menuIdList",menuIdList);
        return Result.build(map,ResultCodeEnum.SUCCESS);
    }


    /**
     * 删除菜单（校验是否存在下级）
     * @param menuId
     * @return
     */
    @DeleteMapping("deleteByMenuId/{menuId}")
    public Result deleteByMenuId(@PathVariable Long menuId){
        sysMenuService.deleteByMenuId(menuId);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }



    @PostMapping("/save")
    public Result save(@RequestBody SysMenu sysMenu) {
        sysMenuService.save(sysMenu);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }


    /**
     * 查询一级菜单列表，并且每个菜单对应下级菜单列表一同查询
     * @return
     */
    @GetMapping("findMenuTree")
    public Result findMenuTree(){
        //最终返回的一级菜单列表
        List<SysMenu> menuList = sysMenuService.findMenuListByParentId(0L);
        return Result.build(menuList, ResultCodeEnum.SUCCESS);
    }
}

